﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FurniturePlacing : MonoBehaviour
{
    // Start is called before the first frame update
    [HideInInspector]
    public bool isEditing;
    [HideInInspector]
    public Sprite GetSprite;
    public Camera mainCamera;
    public Transform target;
    private bool onEditing;
    void Start()
    {
        onEditing = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (!onEditing)
        {
            if (isEditing)
            {
                StartCoroutine(Edit());
                onEditing = true;
            }
        }
        else if (!isEditing && onEditing)
        {
            StopAllCoroutines();
            onEditing = false;
        }
    }

    IEnumerator Edit()
    {
        while (true)
        {
            RaycastHit2D raycastHit2D = Physics2D.Raycast(mainCamera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (raycastHit2D.collider != null)
            {
                if (raycastHit2D.collider.tag == "FurniturePlace")
                {

                    if (Input.GetMouseButton(0))
                    {
                        raycastHit2D.collider.GetComponent<SpriteRenderer>().sprite = GetSprite;
                        raycastHit2D.collider.GetComponent<FurniturePlaced>().Placed();
                        /*
                         这之间做数据库的连接调用
                     
                     
                     */
                    }
                }
            }
            yield return new WaitForEndOfFrame();
        }
    }
}
